package org.huawei260.OD260;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Auther: qingle
 * @Date: 2024/8/19-18:35
 * @Description:
 * @version: 1.0
 */
public class O0008_双十一 {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		// 读取商品价格数组
		String[] pricesInput = scanner.nextLine().split(",");
		int[] prices = new int[pricesInput.length];
		for (int i = 0; i < pricesInput.length; i++) {
			prices[i] = Integer.parseInt(pricesInput[i].trim());
		}
		// 读取购买资金额度
		int R = scanner.nextInt();
//		scanner.close();

		int[] topThree = findTopThree(prices, R);
		if (topThree.length < 3) {
			System.out.println("-1");
		}else{
			System.out.println(topThree[0] + topThree[1] + topThree[2]);
		}
	}

	public static int[] findTopThree(int[] prices, int R) {
		// 对价格数组进行排序
		Arrays.sort(prices);

		for (int i = prices.length - 1; i > 1; i--) {
			if (prices[i] + prices[i - 1] + (i - 2 >= 0 ? prices[i - 2] : 0) <= R) {
				return new int[]{i >= 2 ? prices[i - 2] : -1, prices[i - 1], prices[i]};
			}
		}
		return new int[0]; // Return an empty array if no such three numbers are found
	}

}
